#include <iostream>
#include <string>


using namespace std;


class Solution 
{
public:
    string trans(string s, int n)
    {
        for (int i = 0; i < n; i++)
        {
            if ('A' <= s[i] && s[i] <= 'Z')
            {
                s[i] += 32;
            }
            else if ('a' <= s[i] && s[i] <= 'z')
            {
                s[i] -= 32;
            }
        }
        reverse(s.begin(), s.end());
        int flag = -1;
        for (int j = 0; j < n; j++)
        {
            if (s[j] == ' ')
            {
                reverse(s.begin() + (flag + 1), s.begin() + j);
                flag = j;
            }
            if (j == n - 1)
            {
                reverse(s.begin() + (flag + 1), s.begin() + j + 1);
            }
        }
        
        return s;
    }
};
//ELPMAS A SI SIHt
int main()
{

    Solution xz;
    string a = "This is a sample";
    int n = 16;

    string s = xz.trans(a, n);
    for (auto e : s)
    {
        cout << e;
    }

    return 0;
}